package com.zhang.zhangapp;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.Constant;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.TokenData;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.utils.SPUtils;
import com.zhang.zhangapp.utils.T;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;
import com.zhang.zhangapp.view.IndexFragment;
import com.zhang.zhangapp.view.MineFragment;
import com.zhang.zhangapp.view.NearByFragment;
import com.zhang.zhangapp.view.adapter.FragmentAdapter;
import com.zhang.zhangapp.view.custom.CustomViewPager;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.home_container_layout)
    CustomViewPager viewPager;

    @ViewInject(R.id.home_index_image)
    ImageView homeIndexIamge;

    @ViewInject(R.id.home_location_image)
    ImageView homeLocationIamge;

    @ViewInject(R.id.home_mine_image)
    ImageView homeMineIamge;

    @ViewInject(R.id.home_index_text)
    TextView homeIndexText;

    @ViewInject(R.id.home_location_text)
    TextView homeLocationText;

    @ViewInject(R.id.home_mine_text)
    TextView homeMineText;

    List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initViewPager();
        selectHomeIndex();
    }

    private void initViewPager() {
        fragments.add(new IndexFragment());
        fragments.add(new NearByFragment());
        fragments.add(new MineFragment());
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);
    }


    private void unSelectBottomMenu() {
        homeIndexIamge.setSelected(false);
        homeLocationIamge.setSelected(false);
        homeMineIamge.setSelected(false);

        homeIndexText.setSelected(false);
        homeLocationText.setSelected(false);
        homeMineText.setSelected(false);
    }

    public void selectHomeIndex() {
        unSelectBottomMenu();
        homeIndexIamge.setSelected(true);
        homeIndexText.setSelected(true);
        viewPager.setCurrentItem(0);
    }

    private void selectHomeMine() {
        unSelectBottomMenu();
        homeMineIamge.setSelected(true);
        homeMineText.setSelected(true);
        viewPager.setCurrentItem(2);
    }

    private boolean checkLogin(int tag) {
        if (MApplication.getInstance().getUser() == null) {
            goLoginActivity(tag);
            return false;
        }
        return true;
    }

    @Event(type = View.OnClickListener.class, value = {R.id.home_index_layout, R.id.home_location_layout, R.id.home_mine_layout})
    private void bottomMuneClick(View view) {
        switch (view.getId()) {
            case R.id.home_index_layout:
                if (!homeIndexIamge.isSelected())
                    selectHomeIndex();
                break;
            case R.id.home_location_layout:
                if (homeLocationIamge.isSelected())
                    break;
                unSelectBottomMenu();
                homeLocationIamge.setSelected(true);
                homeLocationText.setSelected(true);
                viewPager.setCurrentItem(1);
                break;
            case R.id.home_mine_layout:
                if (MApplication.getInstance().getUser() == null) {
                    goLoginActivity(Constant.START_LOGIN_GO_MINE);
                    break;
                }
                if (homeMineIamge.isSelected())
                    break;
                selectHomeMine();
                break;
        }
    }

    private void goLoginActivity(int tag) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivityForResult(intent, tag);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constant.START_LOGIN_GO_MINE:
                    selectHomeMine();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.zoomout);
    }

    /**
     *
     */
    public void updateUserInfo() {
        Map<String, String> params = new HashMap<>();
        params.put("mobileNum", MApplication.getInstance().getUser().getMobilenum());
        CommonHttp.commonHttp("hzg/member/queryMember", params, getUserInfo);
    }

    MCallback getUserInfo = new MCallback() {
        @Override
        public void onSuccess(String data) {
            try {
                Gson gson = new Gson();
                TokenData tokenData = gson.fromJson(data, TokenData.class);
                if (tokenData != null) {
                    MApplication.getInstance().setUser(tokenData.getMember(), MApplication.getInstance().getUser().getToken(), tokenData.getTickets());
                }
            } catch (Exception e) {
            }
        }

        @Override
        public void onSuccess(String data, String result) {

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    };
}
